<script type="text/javascript">
var maxpage;
var nowpage;
function dealActive(data) {
	var temp = $("#activetemple").clone();
	temp.attr("id", data["aid"]);
	temp.attr("style", "");
	if (data["userid"] != -1) {
		temp.find(".stuactive.header.content").text(data["date"]).prepend(getUserClickableCode(data["userid"], data["name"]));
		
	} else {
		temp.find(".stuactive.header.content").text(getUserClickableCode(data["date"]));
	}
	temp.find(".ui.mini.stuactive.image").attr("src",data["userImageURL"]);
	temp.find(".stuactive.main.content").text(data["content"]);
	temp.find(".stuactive.commit.id").attr("value", data["aid"]);
	if (data["allowAdmin"]) {
		temp.find("#drop").attr("style", "");
		temp.find(".item.stuactive.delete").attr("onClick", "javascript:deleteRequest(" + data["aid"]+")");
	}
	if (data["has_comment"]==0) {
		temp.find(".stuactive.commit.switch").hide();
	}
	$("#activecontent").prepend(temp);
}
function getUserClickableCode(uid, name) {
	return $("<a style='color: #696969' href='home.php?uid=" + uid + "'>" + name + "</a> nasp");
}
function loadJson(json) {
	for(var o in json){
		if(o!="state"){
			if(o=="maxpage"){
				maxpage=json[o];
				console.log(maxpage);
				continue;
			}
			dealActive(json[o]);
		}
		
	}
}
$(document).ready(function() {
	requestActive(1);
});
function requestActive(page) {
	$("#activecontent").text("");
	nowpage = page;
	$(".ui.text.loader.stuactive").show();
	
	$.ajax({
		type: "GET",
		dataType: "json",
		asnyc: true,
		url: ("home.php?mod=home&node=home&op=listActive&ajax=true&uid=6&page=" + page),
		complete: function(e) {
			
			try {
				var json = eval(e.responseJSON);
				if (json["state"] == 0) {
					$("#activecontent").text("请求失败，服务器返回：" + json["message"]);
				} else {
					loadJson(json);
					updatePageShow();
				}
			} catch(err) {
				$("#activecontent").text("服务器返回的数据无法识别，请将下面这段代码提交给网站开发者。谢谢");
				console.log(err);
				$("#activecontent").append(e.responseText);
			
			}
			$(".ui.text.loader.stuactive").hide();
		}
	});

}
function nextPage(){
	if(!isValidPage(nowpage+1)){
		return false;
	}
	requestActive(nowpage+1);
	return true;
	
}
function upPage(){
	if(!isValidPage(nowpage-1)){
		return;
	}
	requestActive(nowpage-1);
}
function jump(page){
	if(!isValidPage(page)){
		errorShow("无效的请求页面");
		return;
	}
	requestActive(page);
}
function isValidPage(page){
	return (parseInt(page)>0 && parseInt(page)<=parseInt(maxpage));
}
function updatePageShow(){
	$(".item.stuactive.allpage").text("第 "+nowpage+"/"+maxpage+" 页");
}
function deleteRequest(id){
	$.ajax({
		type: "GET",
		dataType: "json",
		asnyc: true,
		url: ("home.php?mod=home&ajax=true&node=home&op=removeactive&aid="+id),
		complete: function(e) {
			try {
				var json = eval(e.responseJSON);console.log(json);
				if (json["state"] == 0) {
					showError("请求失败，服务器返回：" + json["message"]);
				} else {
					resultShow("删除成功");
					$("#"+json["id"]).remove();
				}
			} catch(err) {
				$("#errorcontent").text("服务器返回的数据无法识别，请将下面这段代码提交给网站开发者。谢谢");
				$("#errorcontent").append(e.responseText);
				$("#errorcontent").append("错误原因:");
				$("#errorcontent").append(err);
				$("#errormessage").modal('show');
			}
			$(".ui.text.loader.stuactive").hide();
		}
	});
}

</script>
<h4 class="ui horizontal divider header">
	<i class="user icon"></i> <?php global $isOwner; echo $isOwner ? "我的动态" : "TA的动态";?>
</h4>
<div class="ui menu">
	<div class="ui action input">
		<input type="text" size=3 placeholder="页码">
		<div class="ui button" onClick="javascript:jump()">跳转</div>
	</div>
	<div class="right menu">
		<a class="item" href="javascript:void(0);"
			onClick="javascript:upPage();"><i class="angle left icon"></i></a> <a
			class="item stuactive allpage">共 3/4 页</a> <a class="item"
			href="javascript:void(0);" onClick="javascript:nextPage();"><i
			class="angle right icon"></i></a>
	</div>
</div>
<div id="activecontent">
 <div class="ui text loader stuactive">加载中</div> </div>
<div class="ui menu">
	<div class="ui action input">
		<input type="text" size=3 placeholder="页码">
		<div class="ui button" onClick="javascript:jump()">跳转</div>
	</div>
	<div class="right menu">
		<a class="item" href="javascript:void(0);"
			onClick="javascript:upPage();"><i class="angle left icon"></i></a> <a
			class="item stuactive allpage">共 3/4 页</a> <a class="item"
			href="javascript:void(0);" onClick="javascript:nextPage();"><i
			class="angle right icon"></i></a>
	</div>
</div>


<div class="stuactive form" id="activetemple" style="display: none">
	<!-- 需要在这里改ID -->
	<div class="ui top attached block header">
		<img class="ui mini stuactive image" src="image/testhead.png">
		<div class="stuactive header content">
			<!-- 标题 -->
		</div>
		<div class="ui simple dropdown item" id="drop" style="display: none">
			<i class="dropdown icon"></i>
			<div class="menu">
				<a class="item stuactive delete"> 删除 </a>
			</div>
		</div>
	</div>
	<div class="ui bottom attached segment">
		<div class="stuactive main content">
			<!-- 主要内容 -->
		</div>
		<div class="ui divider"></div>
		<div align="right">
			<a href="javascript:void(0);" class="stuactive commit switch"> 展开评论</a>
		</div>

		<div class="ui comments">
			<!-- 评论区 -->
		</div>
		<form class="ui stuactive reply form" style="padding-top: 0px;">
			<input type="hidden" name="id" value="" class="stuactive commit id">
			<!-- 需要在这里改id -->
			<div class="two fields">
				<div class="twelve wide field">
					<input type="text" name="content">
				</div>
				<div class="three wide field">
					<div class="ui blue labeled submit icon button">
						<i class="icon edit"></i> 回复
					</div>
				</div>
			</div>
		</form>
	</div>
</div>